﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace AngleExpress.software
{
    //韵达韵呼宝
    class YunDaCall : ISoftware
    {

        private readonly string ExePath = System.AppDomain.CurrentDomain.BaseDirectory + @"yd\CTIClient\FSClient.exe";

        public override void OpenSoftware(string username, string password, string otherParam)
        {
            var index = ExePath.LastIndexOf('\\');
            if (index == -1)
            {
                throw new Exception("文件目录路径非法!");
            }
            var workDir = ExePath.Substring(0, index);
            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = ExePath;
            info.WorkingDirectory = workDir;
            try
            {
                Process pro = Process.Start(info);
            }
            catch (Exception e)
            {
                throw e;
            }
            InputSomething(username, password);
        }

        private void InputSomething(string username, string password)
        {
            while (true)
            {
                IntPtr topWindow = GetTopWindow();
                if (topWindow != IntPtr.Zero)
                {
                    var editHandles = GetChildWindows(topWindow);
                    if (editHandles.Count != 7)
                    {
                        Console.WriteLine("continue:" + editHandles.Count);
                        Thread.Sleep(500);
                        continue;
                    }
                    Thread.Sleep(8000);
                    Console.WriteLine("write data");
                    Console.WriteLine(WinApi.SendMessage(editHandles[6], Message.WM_SETTEXT, IntPtr.Zero, Marshal.StringToHGlobalAnsi(username)));
                    Console.WriteLine(WinApi.SendMessage(editHandles[5], Message.WM_SETTEXT, IntPtr.Zero, Marshal.StringToHGlobalAnsi(password)));
                    break;
                } 
                else
                {
                    Console.WriteLine("not find");
                    Thread.Sleep(300);
                }
            }
        }


        private IntPtr GetTopWindow()
        {
            var list = new List<IntPtr>();
            var listHandle = GCHandle.Alloc(list);

            try
            {
                WinApi.EnumWindows(EnumTopWindowsCallback, GCHandle.ToIntPtr(listHandle));
            }
            finally
            {
                Console.WriteLine("查找顶级窗体完毕");
                if (listHandle.IsAllocated)
                    listHandle.Free();
            }

            foreach (IntPtr handle in list)
            {
                if (isLoginWindow(handle))
                {
                    return handle;
                }
            }
            return IntPtr.Zero;
        }

        private bool isLoginWindow(IntPtr parent)
        {
            var list = new List<IntPtr>();
            var listHandle = GCHandle.Alloc(list);

            try
            {
                WinApi.EnumChildWindows(parent, EnumLoginButton, GCHandle.ToIntPtr(listHandle));
            }
            finally
            {
                if (listHandle.IsAllocated)
                    listHandle.Free();
            }
            return list.Count==1;
        }

      
        private bool EnumTopWindowsCallback(IntPtr handle, IntPtr pointer)
        {
            var gcHandle = GCHandle.FromIntPtr(pointer);

            var list = gcHandle.Target as List<IntPtr>;
            var sb = new StringBuilder(200);
            WinApi.GetClassName(handle, sb, 200);
            if (sb.ToString() == "WindowsForms10.Window.8.app.0.1e6fa8e")
            {
                list.Add(handle);
            }

            return true;
        }


        private List<IntPtr> GetChildWindows(IntPtr parent)
        {
            var list = new List<IntPtr>();
            var listHandle = GCHandle.Alloc(list);

            try
            {
                WinApi.EnumChildWindows(parent, EnumChildWindowsCallback, GCHandle.ToIntPtr(listHandle));
            }
            finally
            {
                Console.WriteLine("Finally");
                if (listHandle.IsAllocated)
                    listHandle.Free();
            }
            return list;
        }

        private bool EnumChildWindowsCallback(IntPtr handle, IntPtr pointer)
        {
            var gcHandle = GCHandle.FromIntPtr(pointer);

            var list = gcHandle.Target as List<IntPtr>;

            var sb = new StringBuilder(200);
            WinApi.GetClassName(handle, sb, 200);
            if (sb.ToString() == "WindowsForms10.EDIT.app.0.1e6fa8e")
            {
                list.Add(handle);
            }

            return true;
        }

        private bool EnumLoginButton(IntPtr handle, IntPtr pointer)
        {
            var gcHandle = GCHandle.FromIntPtr(pointer);

            var list = gcHandle.Target as List<IntPtr>;

            var sb = new StringBuilder(200);
            WinApi.GetWindowText(handle, sb, 200);
            if (sb.ToString() == "密  码：")
            {
                list.Add(handle);
            }

            return true;
        }
    }
}
